package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.admin.MarketUserService;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/5/4 11:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    private MarketUserService userService;

    @GetMapping("list")
    //展示所有的用户信息，分页查询
    //逻辑：根据用户输入的查询条件，查询到符合条件的用户信息列表，但是需要注意的是，需要使用分页来操作，并且还需要设定一个排序规则
    public Object list(String page,String limit,String username,String mobile,String sort,String order, HttpSession session) throws IOException {
        //接收用户提交过来的请求参数信息
        Integer pageParam = null;
        Integer limitParam= null;
        try {
            pageParam = Integer.parseInt(page);
            limitParam = Integer.parseInt(limit);
        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            return ResponseUtil.fail();
            //throw new RuntimeException(e);
        }
        //处理具体的业务逻辑
//        List<MarketUser> marketUserList = userService.list(limit,page, username, mobile, sort, order);
        List<MarketUser> marketUserList = userService.list(limitParam,pageParam, username, mobile, sort, order);

        //返回响应
        return  ResponseUtil.okList(marketUserList);

    }
    @PostMapping("/update")
    public Object update(@RequestBody Map map) throws IOException {
        Integer useredUpdate=userService.userUpdate(map);
        //返回响应
        return ResponseUtil.ok(useredUpdate);

    }
    @GetMapping("detail")
    public Object detail(String id) throws IOException {
        MarketUser marketUser= userService.detail(id);
        return ResponseUtil.ok(marketUser);

    }
}
